package com.woniu.carrent.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.carrent.mapper.TBookcarMapper;
import com.woniu.carrent.pojo.TBookcar;
import com.woniu.carrent.pojo.dto.TBookcarDto;
import com.woniu.carrent.pojo.vo.TBookcarVo;
import com.woniu.carrent.util.ResultMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RequestMapping("/tbookcar")
@CrossOrigin
@RestController
@SaCheckLogin
public class TBookcarController {
    @Autowired
    private TBookcarMapper tBookcarMapper;
    /**
     * 查询预约记录
     * @param tbookcarDto
     * @return
     */
    @RequestMapping("/selectBookcar")
    public ResultMessage selectBookcar(@RequestBody TBookcarDto tbookcarDto) {
//        Integer pageNum = tbookcarDto.getPageNum();
//        Integer pageSize = tbookcarDto.getPageSize();
//        Page<TBookcar> page = new Page<>(pageNum, pageSize);
        TBookcar bookcar = new TBookcar();
        BeanUtils.copyProperties(tbookcarDto,bookcar);

        QueryWrapper<TBookcar> queryWrapper = new QueryWrapper<>();
        if (bookcar.getUserid() != null){
            queryWrapper.eq("b.userid",bookcar.getUserid());
        }
        if(bookcar.getStatus() != null){
            queryWrapper.eq("b.status",bookcar.getStatus());
        }
        if (bookcar.getCdid() != null){
            queryWrapper.eq("b.cdid",bookcar.getCdid());
        }
        List<TBookcar> list = tBookcarMapper.selectByUserId(queryWrapper);
        return ResultMessage.success(list);
    }
    /**
     * 生成新预约记录
     * @param tbookcarDto
     */
    @RequestMapping("/insertBookcar")
    public ResultMessage insertBookcar(@RequestBody TBookcarDto tbookcarDto) {
        TBookcar bookcar = new TBookcar();
        BeanUtils.copyProperties(tbookcarDto,bookcar);
        bookcar.setStatus(0);
        bookcar.setCreatetime(new Date());
        bookcar.setUpdatetime(new Date());
        bookcar.setIsdeleted(0);
        int flag = tBookcarMapper.insert(bookcar);
        if (flag==1) {
            return ResultMessage.success("添加成功");
        }
        return ResultMessage.fail("添加失败");
    }
    /**
     * 删除预约记录
     * @param tbookcarDto
     * @return
     */
    @RequestMapping("/deleteBookcar")
    public ResultMessage deleteBookcar(@RequestBody TBookcarDto tbookcarDto) {
        Integer bcid = tbookcarDto.getBcid();
        // 使用ID删除
        boolean flag = tBookcarMapper.deleteByBcid(bcid) > 0;
        if (flag) {
            return ResultMessage.success("删除成功");
        }
        return ResultMessage.fail("删除失败");
    }

    /**
     * 修改预约状态status
     * @param tbookcarDto
     * @return
     */
    @RequestMapping("/updateBookcar")
    public ResultMessage updateBookcar(@RequestBody TBookcarDto tbookcarDto) {
        Integer bcid = tbookcarDto.getBcid();
        Integer status = tbookcarDto.getStatus();
        Integer flag = tBookcarMapper.updateByBcid(bcid,status);
        if (flag==1) {
            return ResultMessage.success("修改成功");
        }
        return ResultMessage.fail("修改失败");
    }
    /**
     * 卖家查询预约记录
     * @param tbookcarDto
     */
    @RequestMapping("/selectBookcarBySeller")
    public ResultMessage selectBookcarBySeller(@RequestBody TBookcarDto tbookcarDto) {
        Integer userid = tbookcarDto.getUserid();
        List<TBookcarVo> list = tBookcarMapper.selectBookcarBySeller(userid);
        if (list != null){
            return ResultMessage.success(list);
        }
        return ResultMessage.fail("查询失败");
    }
}
